package com.tbit.main.service.impl;

import com.tbit.main.pojo.AgentSta;
import com.tbit.main.pojo.DispatchPaySta;
import com.tbit.main.pojo.Point;
import com.tbit.main.pojo.RideSta;
import com.tbit.main.service.*;
import com.tbit.main.util.DateTimeUtil;
import com.tbit.main.util.GeoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author LMY
 * @create 2020-11-27 11:37
 * 分区运营简报2.0
 */
@Service("georeGionOperationService")
public class GeoreGionOperationServiceImpl implements GeoreGionOperationService {
    /**
     * 金额格式化
     */
    private DecimalFormat df = new DecimalFormat("0.00");
    @Autowired
    private MachineService machineService;
    @Autowired
    private RideLogService rideLogService;
    @Autowired
    private DispatchLogService dispatchLogService;
    @Autowired
    private MoveDayStaService moveDayStaService;
    @Autowired
    private PowerDayStaService powerDayStaService;
    @Autowired
    private DispatchDayStaService dispatchDayStaService;
    @Autowired
    private RideOrderDayStaService rideOrderDayStaService;
    @Autowired
    private PowerChangeLogService powerChangeLogService;
    @Autowired
    private MoveLogService moveLogService;
    @Autowired
    private GeoreMachineService georeMachineService;
    @Autowired
    private OperationBriefingService operationBriefingService;
    @Autowired
    private DataService dataService;

    @Override
    public AgentSta getOrder(Integer regionId, String startTime, String endTime) {
        AgentSta agentSta = new AgentSta();
        agentSta.setAccountId(regionId);

        /**调度订单和费用*/
        DispatchPaySta dispatchPaySta = dispatchLogService.staByTime(regionId, startTime, endTime);
        agentSta.setDispatchCount(dispatchPaySta.getDispatchCount());
        if (dispatchPaySta.getDispatchMoney() == null) {
            agentSta.setDispatchMoney("0");
        } else {
            agentSta.setDispatchMoney(df.format(dispatchPaySta.getDispatchMoney() / 100));
        }

        /**骑行订单信息*/
        Map<String, Object> orderCumStaMap = rideOrderDayStaService.getByRegionId(regionId, startTime, endTime);
        if (orderCumStaMap != null) {
            agentSta.setOrderCount(Integer.parseInt(orderCumStaMap.get("orderCount").toString()));
            agentSta.setOrderMoney(df.format(Float.parseFloat(orderCumStaMap.get("orderMoney").toString()) / 100));
        } else {
            agentSta.setOrderCount(0);
            agentSta.setOrderMoney("0.00");
        }

        if (endTime.substring(0, 10).equals(DateTimeUtil.getNowTimeYMD())) {

            /**骑行订单信息 当天
             * */
            RideSta rideStaOrderCumSta = rideLogService.staRegionIdByTime(regionId, DateTimeUtil.getNowDateTimeBefore(0) + " 00:00:00", DateTimeUtil.getNowTime());
            if (rideStaOrderCumSta.getOrderCount() == null) {
                rideStaOrderCumSta.setOrderCount(0);
            }
            if (rideStaOrderCumSta.getOrderMoney() == null) {
                rideStaOrderCumSta.setOrderMoney(0.00);
            } else {
                rideStaOrderCumSta.setOrderMoney((double) rideStaOrderCumSta.getOrderMoney() / 100);
            }
            agentSta.setOrderCount(agentSta.getOrderCount() + rideStaOrderCumSta.getOrderCount());
            agentSta.setOrderMoney(df.format((Double.parseDouble(agentSta.getOrderMoney()) + rideStaOrderCumSta.getOrderMoney())));

        }

        if ((0 <= DateTimeUtil.getNewHour() && DateTimeUtil.getNewHour() <= 3) &&
                DateTimeUtil.isAfterToday(DateTimeUtil.getDayAgoTime(1), startTime) &&
                DateTimeUtil.isBeforeToday(DateTimeUtil.getDayAgoTime(1), endTime)) {

            /**骑行订单信息 前天
             * */
            Map<String, Object> orderCumStaMapT = rideOrderDayStaService.getByRegionId(regionId,
                    DateTimeUtil.getNowDateTimeBefore(-1) + " 00:00:00", DateTimeUtil.getNowDateTimeBefore(-1) + " 23:59:59");
            if (orderCumStaMapT == null || Integer.parseInt(orderCumStaMapT.get("orderCount").toString()) == 0) {
                RideSta rideStaOrderCumSta = rideLogService.staRegionIdByTime(regionId,
                        DateTimeUtil.getNowDateTimeBefore(-1) + " 00:00:00", DateTimeUtil.getNowDateTimeBefore(-1) + " 23:59:59");
                if (rideStaOrderCumSta.getOrderCount() == null) {
                    rideStaOrderCumSta.setOrderCount(0);
                }
                if (rideStaOrderCumSta.getOrderMoney() == null) {
                    rideStaOrderCumSta.setOrderMoney(0.00);
                } else {
                    rideStaOrderCumSta.setOrderMoney((double) rideStaOrderCumSta.getOrderMoney() / 100);
                }
                agentSta.setOrderCount(agentSta.getOrderCount() + rideStaOrderCumSta.getOrderCount());
                agentSta.setOrderMoney(df.format((Double.parseDouble(agentSta.getOrderMoney()) + rideStaOrderCumSta.getOrderMoney())));
            }
        }

        return agentSta;
    }

    @Override
    public AgentSta getOperation(Integer accountId, Integer regionId, String startTime, String endTime) {

        List<Integer> regionIds = new LinkedList<>();
        regionIds.add(regionId);

        AgentSta agentSta = new AgentSta();
        agentSta.setAccountId(regionId);

        //车辆总数
        Integer countList = georeMachineService.getRegionIdMachine(regionId);
        if (countList == null) {
            agentSta.setMachineCount(0);
        } else {
            agentSta.setMachineCount(countList);
        }

        agentSta.setMachineUserCount(operationBriefingService.selectOperationalMachineCountByAccountIdOrRegionId(accountId, regionId));

        /**分区区域面积*/
        List<Point> points = dataService.getGeore(regionId);

        Integer accountIdArea = GeoUtil.pointArea(points);

        /**运营区域密度*/
        if (Objects.equals(0, accountIdArea)) {
            agentSta.setAccountIDensity((float) agentSta.getMachineCount() * 10000);
        } else {
            DecimalFormat df = new DecimalFormat("0.00");

            /**兼容分区特别小，默认使用车辆数*/
            if ((accountIdArea / 10000) == 0) {
                agentSta.setAccountIDensity((float) agentSta.getMachineCount());
            } else {
                agentSta.setAccountIDensity(Float.parseFloat(df.format((float) agentSta.getMachineCount() /
                        (accountIdArea / 10000))));
            }
        }
        //运营区域面积 由于前端是除于1000000并保留两位，所以这里取万位以上的数据
        agentSta.setAccountIdArea((accountIdArea / 10000) * 10000);

        return agentSta;
    }

    @Override
    public AgentSta getOPS(Integer regionId, String startTime, String endTime) {
        AgentSta agentSta = new AgentSta();
        agentSta.setRegionId(regionId);

        /**换电总次数*/
        Integer powerChangeCount = powerDayStaService.getByRegionId(regionId, startTime, endTime);
        if (powerChangeCount == null) {
            agentSta.setPowerChangeCount(0);
        } else {
            agentSta.setPowerChangeCount(powerChangeCount);
        }

        /**调度总次数*/
        Integer dispatchLogCount = dispatchDayStaService.getByRegionId(regionId, startTime, endTime);
        if (dispatchLogCount == null) {
            agentSta.setDispatchCounts(0);
        } else {
            agentSta.setDispatchCounts(dispatchLogCount);
        }

        /**挪车总次数*/
        Integer moveLogCount = moveDayStaService.getByRegionId(regionId, startTime, endTime);
        if (moveLogCount == null) {
            agentSta.setMoveCount(0);
        } else {
            agentSta.setMoveCount(moveLogCount);
        }

        if (endTime.substring(0, 10).equals(DateTimeUtil.getNowTimeYMD())) {
            /**换电总次数*/
            Integer powerChangeCountNew = powerChangeLogService.getByRegionIdTime(regionId, DateTimeUtil.getNowDateTimeBefore(0) + " 00:00:00", endTime);
            if (powerChangeCountNew == null) {
                agentSta.setPowerChangeCount(agentSta.getPowerChangeCount() + 0);
            } else {
                agentSta.setPowerChangeCount(agentSta.getPowerChangeCount() + powerChangeCountNew);
            }

            /**调度总次数*/
            Integer dispatchLogCountNew = dispatchLogService.getByRegionIdTime(regionId, 1, DateTimeUtil.getNowDateTimeBefore(0) + " 00:00:00", endTime, 1);
            if (dispatchLogCountNew == null) {
                agentSta.setDispatchCounts(agentSta.getDispatchCounts() + 0);
            } else {
                agentSta.setDispatchCounts(agentSta.getDispatchCounts() + dispatchLogCountNew);
            }

            /**挪车总次数*/
            Integer moveLogCountNew = moveLogService.groupByRegionIdTime(regionId, true, DateTimeUtil.getNowDateTimeBefore(0) + " 00:00:00", endTime);
            if (moveLogCountNew == null) {
                agentSta.setMoveCount(agentSta.getMoveCount() + 0);
            } else {
                agentSta.setMoveCount(agentSta.getMoveCount() + moveLogCountNew);
            }
        }

        if ((0 <= DateTimeUtil.getNewHour() && DateTimeUtil.getNewHour() <= 3) &&
                DateTimeUtil.isAfterToday(DateTimeUtil.getDayAgoTime(1), startTime) &&
                DateTimeUtil.isBeforeToday(DateTimeUtil.getDayAgoTime(1), endTime)) {

            Integer powerChangeCountT = powerDayStaService.getByRegionId(regionId, DateTimeUtil.getNowDateTimeBefore(-1) + " 00:00:00", DateTimeUtil.getNowDateTimeBefore(-1) + " 23:59:59");
            if (powerChangeCountT == null || powerChangeCountT == 0) {
                /**换电总次数前天*/
                Integer powerChangeCountNew = powerChangeLogService.getByRegionIdTime(regionId, DateTimeUtil.getNowDateTimeBefore(-1) + " 00:00:00", DateTimeUtil.getNowDateTimeBefore(-1) + " 23:59:59");
                if (powerChangeCountNew != null) {
                    agentSta.setPowerChangeCount(agentSta.getPowerChangeCount() + powerChangeCountNew);
                }
            }

            /**调度总次数*/
            Integer dispatchLogCountT = dispatchDayStaService.getByRegionId(regionId, DateTimeUtil.getNowDateTimeBefore(-1) + " 00:00:00", DateTimeUtil.getNowDateTimeBefore(-1) + " 23:59:59");
            if (dispatchLogCountT == null || dispatchLogCountT == 0) {
                /**调度总次数前天*/
                Integer dispatchLogCountNew = dispatchLogService.getByRegionIdTime(regionId, 1, DateTimeUtil.getNowDateTimeBefore(-1) + " 00:00:00", DateTimeUtil.getNowDateTimeBefore(-1) + " 23:59:59", 1);
                if (dispatchLogCountNew != null) {
                    agentSta.setDispatchCounts(agentSta.getDispatchCounts() + dispatchLogCountNew);
                }
            }

            /**挪车总次数*/
            Integer moveLogCountT = moveDayStaService.getByRegionId(regionId, DateTimeUtil.getNowDateTimeBefore(-1) + " 00:00:00", DateTimeUtil.getNowDateTimeBefore(-1) + " 23:59:59");
            if (moveLogCountT == null || moveLogCountT == 0) {
                /**挪车总次数前天*/
                Integer moveLogCountNew = moveLogService.groupByRegionIdTime(regionId, true, DateTimeUtil.getNowDateTimeBefore(-1) + " 00:00:00", DateTimeUtil.getNowDateTimeBefore(-1) + " 23:59:59");
                if (moveLogCountNew != null) {
                    agentSta.setMoveCount(agentSta.getMoveCount() + moveLogCountNew);
                }
            }
        }

        return agentSta;
    }
}
